package controllers

import (
	"encoding/json"
	"fmt"
	"io"
	"merge_Web/models"
	"os"

	beego "github.com/beego/beego/v2/server/web"
)

type ServerController struct {
	beego.Controller
}

func (c *ServerController) Get() {
	c.Data["Website"] = "beego.vip"
	c.Data["Email"] = "astaxie@gmail.com"
	c.TplName = "index.tpl"

}

type ThirdController struct {
	beego.Controller
}

func (c *ThirdController) GetAndroidVersion() {
	code := models.GameBuildCode{}

	orm := models.GetNewOrm()
	version := &models.ServerVersionData{}
	err1 := orm.Raw("SELECT * FROM t_Server_Version_Data WHERE Platfrom = ?", "Android").QueryRow(version)

	// BuildCode, err := beego.AppConfig.Int("AndroidBuildCode")
	// VersionName, err1 := beego.AppConfig.String("AndroidVersionName")
	if err1 == nil {
		code.ResultCode = 0
		code.BuildCode = version.BuildCode
		code.Platform = version.Platfrom
		code.VersionName = version.VersionName
		code.Content = version.Content
	} else {
		code.ResultCode = 1
	}
	c.Data["json"] = &code
	c.ServeJSON()
}

func (c *ThirdController) GetIOSVersion() {
	code := models.GameBuildCode{}
	orm := models.GetNewOrm()
	version := &models.ServerVersionData{}
	err1 := orm.Raw("SELECT * FROM t_Server_Version_Data WHERE Platfrom = ?", "IOS").QueryRow(version)
	if err1 == nil {
		code.ResultCode = 0
		code.BuildCode = version.BuildCode
		code.Platform = version.Platfrom
		code.VersionName = version.VersionName
		code.Content = version.Content
	} else {
		code.ResultCode = 1
	}
	c.Data["json"] = &code
	c.ServeJSON()
}

var All_Counts map[int32]*models.TAllAccount = make(map[int32]*models.TAllAccount)

type PlayerController struct {
	beego.Controller
}

func (this *PlayerController) GetPlayerInfoById() {

	o := models.GetNewOrm()
	var ob models.PlayerAccountId
	var err error
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &ob); err == nil {
		account, ok := All_Counts[ob.DwUin]
		if ok {
			this.Data["json"] = &account
		} else {
			var account models.TAllAccount = models.TAllAccount{DwUin: ob.DwUin}
			err1 := o.Raw("SELECT * FROM t_all_account WHERE dwUin = ?", ob.DwUin).QueryRow(&account)
			if err1 != nil {
				account.Result = 1
			} else {
				account.Result = 0
			}

			this.Data["json"] = &account
		}

	} else {
		this.Data["json"] = err.Error()
	}
	this.ServeJSON()
}

func (this *PlayerController) RegisterPlayerUin() {
	o := models.GetNewOrm()
	var ob models.PlayerAccountInfo
	var err error
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &ob); err == nil {
		var account models.TAllAccount = models.TAllAccount{UserName: ob.UserName}
		err1 := o.Raw("SELECT * FROM t_all_account WHERE user_name = ?", ob.UserName).QueryRow(&account)
		if err1 != nil {
			account.Result = 1
		} else {

			account.DwUin = ob.DwUin
			account.Result = 0
			o.Update(&account)
		}
		// js, _ := json.Marshal(&account)
		this.Data["json"] = &account
	}
	this.ServeJSON()
}

func (this *PlayerController) GetPlayerInfo() {

	o := models.GetNewOrm()
	var ob models.PlayerAccountInfo
	var err error
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &ob); err == nil {
		var account models.TAllAccount = models.TAllAccount{UserName: ob.UserName, UserPassword: ob.UserPassword}
		err1 := o.Raw("SELECT * FROM t_all_account WHERE user_name = ? and user_password = ?", ob.UserName, ob.UserPassword).QueryRow(&account)
		if err1 != nil {
			account.UserName = ob.UserName
			account.UserPassword = ob.UserPassword
			account.Platform = ob.Platform

			account.IpAddress, _ = beego.AppConfig.String("cursvrip")
			account.TcpPort, _ = beego.AppConfig.String("TcpPort")
			id, err2 := o.Insert(&account)
			if err2 == nil {
				account.Result = 1
			} else {
				account.Result = 2
			}
			account.AutoId = int32(id)

		} else {
			account.Result = 0
		}
		this.Data["json"] = &account
	} else {
		this.Data["json"] = err.Error()
	}
	this.ServeJSON()
}

func (this *PlayerController) FindPlayerInfo() {
	o := models.GetNewOrm()
	var ob models.PlayerAccountInfo
	var err error
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &ob); err == nil {
		var account models.TAllAccount = models.TAllAccount{UserName: ob.UserName, UserPassword: ob.UserPassword}
		err1 := o.Raw("SELECT * FROM t_all_account WHERE user_name = ? and user_password = ?", ob.UserName, ob.UserPassword).QueryRow(&account)
		if err1 != nil {
			account.Result = 2
		} else {
			account.AutoId = 0
			account.Result = 0
		}
		this.Data["json"] = &account
	} else {
		this.Data["json"] = err.Error()
	}
	this.ServeJSON()
}

func (this *PlayerController) ReqAllAddFriendInfo() {
	o := models.GetNewOrm()
	var ob models.HttpReqSqlFriendStruct
	var err error
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &ob); err == nil {
		var st *models.HttpRetSqlAddFriendStruct = &models.HttpRetSqlAddFriendStruct{}
		st.Datas = []models.SqlAddFriendStruct{}
		var err1 error
		if ob.ReceiverId == 0 && ob.SenderId != 0 {
			_, err1 = o.Raw("SELECT * FROM t_player_Req_add_Friend WHERE SenderId = ?", ob.SenderId).QueryRows(&st.Datas)
		}

		if ob.ReceiverId != 0 && ob.SenderId == 0 {
			_, err1 = o.Raw("SELECT * FROM t_player_Req_add_Friend WHERE ReceiverId = ?", ob.ReceiverId).QueryRows(&st.Datas)
		}

		if ob.ReceiverId != 0 && ob.SenderId != 0 {
			_, err1 = o.Raw("SELECT * FROM t_player_Req_add_Friend WHERE SenderId = ? AND ReceiverId = ?", ob.SenderId, ob.ReceiverId).QueryRows(&st.Datas)
		}

		if err1 != nil {
			st.Result = 1
		} else {

			st.Result = 0
		}
		this.Data["json"] = st
	} else {
		this.Data["json"] = err.Error()
	}
	this.ServeJSON()
}

func (this *PlayerController) ReqAddFriendData() {
	o := models.GetNewOrm()
	var ob models.SqlAddFriendStruct
	var err error
	if err = json.Unmarshal(this.Ctx.Input.RequestBody, &ob); err == nil {
		var st *models.CommonResult = &models.CommonResult{}
		insertId, err1 := o.Insert(&ob)

		if err1 != nil {
			st.Result = 1
			st.InsertId = 0
		} else {
			st.InsertId = int32(insertId)
			st.Result = 0
		}
		this.Data["json"] = st
	} else {
		this.Data["json"] = err.Error()
	}
	this.ServeJSON()
}

type ReadCfgController struct {
	beego.Controller
}

func (c *ReadCfgController) ReadConfig() {

	confPath := string(c.Ctx.Input.RequestBody)
	fmt.Println("文件的地址:")
	fmt.Println(confPath)
	content, err := ReadFile(confPath)
	if err != nil {
		fmt.Println(err)
	} else {
	}
	fmt.Println(content)
	c.Ctx.WriteString(content)
}

// 解析text文件内容
func ReadFile(path string) (str string, err error) {
	//打开文件的路径
	fi, err := os.Open(path)
	if err != nil {
		fmt.Println("打开文件失败")
		fmt.Println(err)
	}
	defer fi.Close()
	//读取文件的内容
	fd, err := io.ReadAll(fi)
	if err != nil {
		fmt.Println("读取文件失败")
		fmt.Println(err)
	}
	str = string(fd)
	return str, err
}
